package ch.ffhs.activities;

import java.lang.reflect.Field;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.Switch;
import ch.ffhs.services.ServiceManager;

/**
 * class MainActivity. Implements the main activity, displays the state of the
 * app and provides the options-menu.
 * 
 * @author mf
 * @author td
 */
public class MainActivity extends Activity {

	// service manager
	private ServiceManager serviceManager = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getOverflowMenu();

		// load ServiceManager
		serviceManager = new ServiceManager(this);

		// setup contact-list
		ListView contactsList = (ListView) findViewById(R.id.listContacts);
		contactsList.setAdapter(serviceManager.contactListAdapter);

		serviceManager.loadSettings();

		// set up switchModus
		Switch switchModus = (Switch) findViewById(R.id.switchModus);
		switchModus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// de-/activate or change modus
				serviceManager.stop();
				serviceManager.setModus(((Switch) buttonView).isChecked());
				serviceManager.loadSettings();
				serviceManager.start();
			}
		});

		// start service-manager
		serviceManager.start();
	}

	@Override
	protected void onDestroy() {
		serviceManager.stop();

		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// add options-menu
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			// display settings
			this.startActivityForResult(
					new Intent(this, SettingsActivity.class),
					SettingsActivity.SETTINGS_REQUEST);
			return true;
		case R.id.action_help:
			startActivity(new Intent(this, HelpActivity.class));
			return true;
		case R.id.action_testalarm:
			serviceManager.startAlarm();
			return true;
		case R.id.action_webservice:
			serviceManager.submitState(ServiceManager.ALARM_LEVEL_NONE);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onClickRestart(final View sfNormal) {
		serviceManager.stop();
		serviceManager.loadSettings();
		serviceManager.start();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_CANCELED) {
			return;
		}

		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case ServiceManager.ALARM_REQUEST:
				serviceManager.stopAlarm();
				break;
			case SettingsActivity.SETTINGS_REQUEST:
				// settings changed, restart service
				serviceManager.stop();
				serviceManager.loadSettings();
				serviceManager.start();
				break;
			}
		}
	}

	private void getOverflowMenu() {
		// set the overflow menu (design specific)
		try {
			ViewConfiguration config = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			if (menuKeyField != null) {
				menuKeyField.setAccessible(true);
				menuKeyField.setBoolean(config, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
